package models

import (
	"github.com/astaxie/beego/orm"
	"fmt"
	"github.com/gin-gonic/gin/json"
	"strconv"
	"github.com/kataras/go-errors"
	"luckapi/mutils"
)

//type Topic struct {
//	Id      	 int   	    `orm:"column(id);auto"`
//	Question 	 string 	`orm:"column(question);size(200)"`
//	Popularity   float32 	`orm:"column(money)"`
//}

type Notice struct {
	Id 		int 	`orm:"column(id);auto"`
	Content string 	`orm:"column(content);size(200)"`
}

type Game struct {
	Id 			int 	`orm:"column(id);auto"`
	Star_time 	string 	`orm:"column(start_time);size(200)"`   //开始时间
	End_time 	string 	`orm:"column(end_time);size(200)"`     //结束时间
	Money   	int     `orm:"column(money)"`                 //奖池金额
	Topic	    string	`orm:"column(topic);size(200)"`
	Game_num    string  `orm:"column(game_num);size(200)"`
    Join_gold   int     `orm:"column(join_gold)"`
    Luck_num    string  `orm:"column(luck_num);size(200)"`
    Luck_gold   int     `orm:"column(luck_gold)"`
    Luck_time   string 	`orm:"column(luck_time);size(200)"`
	Status 		int 	`orm:"column(status)"`
	Game_level  int     `orm:"column(game_level)"`
	Max_gold    int     `orm:"column(max_gold)"`
}

func init() {
	// 需要在 init 中注册定义的 model
	orm.RegisterModel(new(Notice), new(Game))
}

func GetGameListOpen() (apiError mutils.ApiError,gamelist []map[string]string){
	o := orm.NewOrm()
	var games []Game
	_, err := o.QueryTable("game").Filter("status",3).All(&games)
	if err != nil {
		return mutils.ApiErrorWithErrorInfo(err,mutils.ErrorCode_Sql_Fail),nil
	}
	gamelist = make([] map[string]string, len(games))
	if len(games) > 0 {
		for i := 0; i <len(games); i++ {
			m := make(map[string]string)
			m["gamenum"] = games[i].Game_num
			m["startime"] = games[i].Star_time
			m["joingold"] = strconv.Itoa(games[i].Join_gold)
			m["status"] = strconv.Itoa(games[i].Status)
			m["gamelevel"] = strconv.Itoa(games[i].Game_level)
			m["money"] = strconv.Itoa(games[i].Money)
			m["topic"] = games[i].Topic
			m["maxgold"] = strconv.Itoa(games[i].Max_gold)
			gamelist[i] = m
		}
		apiError = mutils.SuccessApiError()
	}else {
		apiError = mutils.ApiErrorWithCode(mutils.ErrorCode_Game_Null)
	}
	return apiError,gamelist
}

func GetNoticeList()  {
	o := orm.NewOrm()
	var t_notices []Notice
	_, err := o.QueryTable("notice").All(&t_notices)
	if err == nil {
		b, _ := json.Marshal(t_notices)
		fmt.Println(string(b))
	}
}


func AddGame(game *Game) (bool bool,message string){
	o := orm.NewOrm()
	o.Using("default")

	var games []Game
	_, err := o.QueryTable("game").Filter("status",game.Status).Filter("game_level",game.Game_level).All(&games)
	if err != nil {
		fmt.Println(err)
		return false,"数据库操作失败1"
	}
	if len(games) > 0 {
		b, _ := json.Marshal(games)
		fmt.Println(string(b))
		return false,"同级别场次正在开启"
	}

	_, err1 := o.Insert(game)
	if err1 != nil {
		fmt.Println(err1)
		return false,"添加场次失败"
	}

	return true,"添加场次数据成功"
}

func UpdateGameGold(gold int, game Game)  (err error) {
	o := orm.NewOrm()
	err = o.Read(&game)
	if err == nil {
		maxgold := game.Money + gold
		if maxgold <= game.Max_gold{
			game.Money = maxgold
			_, err = o.Update(&game,"money")
		}else {
			err = errors.New("场次金额已满，关闭投注")
		}
		return err
	}
	return err
}

